package com.hd.assistant.web.common.base;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.BindException;
import java.net.HttpURLConnection;
import java.net.URL;

import com.hd.assistant.web.common.util.ChangeCharset;

public class BasePageAnalysis {
	public  String getContent(String url,String charset) throws BindException {
		InputStream in = null;
		String s = "";
		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setRequestProperty("Connection", "close");
			in = conn.getInputStream();
			// 开始读取内容正文。
			BufferedReader br = new BufferedReader(new InputStreamReader(in,charset));
			StringBuffer sb = new StringBuffer();
			char[] charBuf = new char[1024 * 4];
			int len = br.read(charBuf);
			while (len != -1) {
				sb.append(charBuf, 0, len);
				len = br.read(charBuf);
			}
			br.close();
			s = sb.toString();
		} catch (BindException e) {
			throw e;
		} catch (Exception e) {
			// 其他异常暂不做处理 直接返回空
			return s;
		} finally {
			if (conn != null)
				conn.disconnect();
		}
		return s;
	}
	
	public static void main(String[] args) {
		BasePageAnalysis b = new BasePageAnalysis();
		long s = System.currentTimeMillis();
		for(int i =0;i<100000;i++){
			try {
				if(i%3==0)
					b.getContent("http://112.124.8.57//redirect.htm?cid=P_D653310A00000000040100002C318E3D","gbk");
				else if(i%10 == 0)
					b.getContent("http://112.124.8.57//redirect.htm?cid=P_D653310A00000000040100002C319DA6","gbk");
				else
					b.getContent("http://112.124.8.57//redirect.htm?cid=P_D653310A00000000040100002C337B8C","gbk");
					
			} catch (BindException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		long e = System.currentTimeMillis();
		System.out.println(e-s);
	}
	
}
